package com.iptv.grape.domain;

import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.net.Entity.ChannelData;
import com.iptv.grape.net.NetworkService;
import com.iptv.grape.util.AppLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ChannelDLinkList {


    // 直播播放历史
    private ChannelHis channelHis;

    public ChannelHis getChannelHis() {
        return channelHis;
    }

    // 频道列表
    private List<Channel> channels = new ArrayList<Channel>();
    // 获得频道列表
    public List<Channel> getChannels() {
        return channels;
    }
    // 返回频道列表个数
    public int size(){
        return this.channels.size();
    }

    // 当前正在操作的频道对象
    private Channel curChannel;

    public Channel getCurChannel(){
        return curChannel;
    }

    public void setChannelHis(Channel channel){
        curChannel = channel;
        getChannelHis().setChannelHis(channel.toChannelRecords());
    }

    public void readFileInit(){
        List<ChannelData> resultList = ChannelData.unSerializeToFile();
        if(resultList != null) {
            init(resultList, false);
        }else{
            List<ChannelData> result = NetworkService.getInstance().getChannelListAsync();
            if(result != null && result.size() > 0){
                init(result, true);
            }
        }
    }

    // 同步当前的频道信息
    public boolean syncData(){
        try {
            List<ChannelData> resultList = NetworkService.getInstance().getChannelListAsync();
            if (resultList == null || resultList.size() == 0) {
                return false;
            }
            ChannelData.serializeToFile(resultList);
            List<Channel> tmpChannels = new ArrayList<Channel>();
            for (int i = 0; i < resultList.size(); i++) {
                ChannelData cData = resultList.get(i);
                Channel channel = this.isExists(tmpChannels, cData);
                if (channel == null) {
                    channel = new Channel(cData);
                } else {
                    channel.addCategoryCode(cData.getCategoryCode());
                }
                tmpChannels.add(channel);
            }
            Collections.sort(tmpChannels);

            // 初始化环形列表
            for (int i = 0; i < tmpChannels.size(); i++) {
                Channel channel = tmpChannels.get(i);
                int nextIndex = 0;
                int previous = tmpChannels.size() - 1;
                if (i + 1 < tmpChannels.size()) {
                    nextIndex = i + 1;
                }
                if (i - 1 >= 0) {
                    previous = i - 1;
                }
                channel.setNext(tmpChannels.get(nextIndex));
                channel.setPrevious(tmpChannels.get(previous));
            }
            channelHis.syncData(tmpChannels);
            channels = tmpChannels;
            GrapeContext.curGrapeContext().syncHotChannelList();
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    // 初始化函数
    public void init(List<ChannelData> resultList,boolean isSavefile){
        if(isSavefile){
            ChannelData.serializeToFile(resultList);
        }
        channels.clear();
        for(int i = 0; i < resultList.size(); i++){
            ChannelData cData = resultList.get(i);
            Channel channel = this.isExists(cData);
            if(channel == null){
                channel = new Channel(cData);
            }
            else {
                channel.addCategoryCode(cData.getCategoryCode());
            }
            channels.add(channel);
        }
        Collections.sort(channels);

        // 初始化环形列表
        for(int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            int nextIndex = 0;
            int previous = channels.size() - 1;
            if(i + 1 < channels.size()){
                nextIndex = i + 1;
            }
            if(i - 1 >= 0){
                previous = i - 1;
            }
            channel.setNext(channels.get(nextIndex));
            channel.setPrevious(channels.get(previous));
        }

        // 初始化播放历史
        channelHis = ChannelHis.unSerializeToFile();
        if(channelHis.isEmpty() && channels.size() > 0){
            // 如果一个空的
            AppLog.getCurLog().out("空的历史");
            channelHis.init(channels.get(0));
            this.curChannel = channels.get(0);
        }else{
            AppLog.getCurLog().out("非空的历史");
            Channel tmpChannel = findChannelByID(channelHis.getCurPlayChannel().getChannelID());
            if(tmpChannel == null){
                tmpChannel = channels.get(0);
                channelHis.reSetLastPlayChannel(tmpChannel);
            }
            this.curChannel = tmpChannel;

        }

    }

    // 根据ID来找到对应的频道
    public Channel findChannelByID(String channelID){
        for(Channel channel: this.channels){
            if(channel.getKeyID().equals(channelID)){
                return channel;
            }
        }
        return null;
    }

    // 根据频道号来找到对应的频道
    public Channel findChannelByNo(int channelNo){
        for(Channel channel: this.channels){
            if(channel.getChannelNo() == channelNo){
                return channel;
            }
        }
        return null;
    }

    public List<Channel> seachByCategoryCode(String categoryCode){
        Channel.setCurType("live");
        if(categoryCode.equals("ALL")){
            return this.channels;
        }
        List<Channel> seachList = new ArrayList<Channel>();
        for(int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            if(channel.getCategoryCodeList().contains(categoryCode)) {
                seachList.add(channel);
            }
        }
        return seachList;
    }

    public List<Channel> seachByCategoryCodeAndHis(String categoryCode){
        Channel.setCurType("his");
        if(categoryCode.equals("ALL")){
            return this.channels;
        }
        List<Channel> seachList = new ArrayList<Channel>();
        for(int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            if(channel.getCategoryCodeList().contains(categoryCode)) {
                seachList.add(channel);
            }
        }

        return seachList;
    }

    private Channel isExists(ChannelData cData){
        for(int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            if(channel.getKeyID().equals(cData.getKeyID())){
                return channel;
            }
        }
        return null;
    }

    private Channel isExists(List<Channel> channelList, ChannelData cData){
        for(int i = 0; i < channelList.size(); i++) {
            Channel channel = channelList.get(i);
            if(channel.getKeyID().equals(cData.getKeyID())){
                return channel;
            }
        }
        return null;
    }
}
